#include "Menu.h"
#include <stdio.h>


static const char *var_menu[MAX_VARIABLES] = {"Cadence","Distance","Pushes","Speed","Reset","Pong","Graph","Log","Clock","Off"};
char *val_disp[MAX_VARIABLES] = {"56","2.0","50","3.0","  ","  ","  ","  ","  "," "};
static const char *time_scale[4][4] ={{"0","6a","12","6p"},{"0","M","W","F"},{"1-6","8-6","16-6","24-6"},{"Jan","Apr","Jul","Oct"}}; 
static const char time_divisions[4] = {DAY_SIZE,WEEK_SIZE,MONTH_SIZE,YEAR_SIZE};

static const char _row1 = 3;
static const char _row2 = 3+11+TEXT_SPACING;
static const char _row3 = 3+2*(11+TEXT_SPACING);
static const char _row4 = 3+3*(11+TEXT_SPACING);
unsigned char _current_row = 0;

//
//
////==============================================================================================
////=================================  Menu Functions  ===========================================
////==============================================================================================
//
//void ClearGraph(void)
//{
//	int row,col;
//	for (row = GRAPH_START; row<=GRAPH_END;row++)
//	{
//		for (col = 0;col<=OLED_END;col++) ucpDispBuff[row][col] = 0;
//	}
//}
//
//void ClearTimes(void)
//{
//	int row,col;
//	for (row = TIME_HEIGHT; row<=OLED_END;row++)
//	{
//		for (col = 0;col<=OLED_END;col++) ucpDispBuff[row][col] = 0;
//	}
//}
//
//unsigned char ScrollVariables(unsigned char _dir,unsigned char _spd)
//{
//	ClearWindow(0,0,127,35);
//	if (_dir==DOWN) 
//	{
//		_current_row = 0;
//		unsigned char _new_row = 15;
//		
//		unsigned char row_string = var_selection - 1;
//		if (row_string >(MAX_VARIABLES-1)) row_string = (MAX_VARIABLES-1);
//
//		UpdateText(_row2,row_string,0xFF);
//		
//		row_string = row_string - 1;
//		if (row_string >(MAX_VARIABLES-1)) row_string = (MAX_VARIABLES-1);
//		
//		UpdateText(_row1,row_string,0xFF);
//		
//		var_selection = (var_selection+1)%MAX_VARIABLES;
//		
//		row_string = var_selection - 1;
//		if (row_string >(MAX_VARIABLES-1)) row_string = (MAX_VARIABLES-1);
//		
//		UpdateText(_row3,row_string,0xFF);
//		UpdateText(_row4,var_selection,0xFF);
//		
//		while (_current_row<_new_row || (_new_row < TEXT_HEIGHT && _current_row > _new_row))
//		{
//			_current_row = (_current_row+_spd)%60;
//			DrawVariables();
//		}
//		while (_current_row!=_new_row)
//		{
//			_current_row--;
//			DrawVariables();
//		}
//	}
//	else 
//	{
//		_current_row = 15;
//		unsigned char _new_row = 0;
//		
//		var_selection--;
//		if (var_selection >(MAX_VARIABLES-1)) var_selection = (MAX_VARIABLES-1);
//		
//		unsigned char row_string = var_selection - 1;
//		if (row_string >(MAX_VARIABLES-1)) row_string = (MAX_VARIABLES-1);
//		
//		UpdateText(_row2,row_string,0xFF);
//		UpdateText(_row3,var_selection,0xFF);
//		UpdateText(_row4,(var_selection+1)%(MAX_VARIABLES),0xFF);
//		
//		row_string--;
//		if (row_string >(MAX_VARIABLES-1)) row_string = (MAX_VARIABLES-1);
//		
//		UpdateText(_row1,row_string,0xFF);
//		
//		while (_current_row>_new_row || (_new_row > _current_row && _new_row > (60 - TEXT_HEIGHT)) )
//		{
//			_current_row-=_spd;
//			if (_current_row>60) _current_row = 60;
//			DrawVariables();
//		}
//		while (_current_row!=_new_row)
//		{
//			_current_row++;
//			DrawVariables();
//		}
//	}
//	return var_selection;
//}
//
//unsigned char ScrollTimes(unsigned char _dir)
//{
//	if (_dir==DOWN) time_selection = (time_selection+1)%(NUM_MEASUREMENTS);
//	else 
//	{
//		time_selection--;
//		if (time_selection >(NUM_MEASUREMENTS-1)) time_selection = (NUM_MEASUREMENTS-1);
//	}
//	return time_selection;
//}
//
//void DrawTimes(unsigned char clr)
//{
//	int i;
//	ClearTimes();
//	for (i=0;i<4;i++)
//	{
//		if (var_selection<NUM_MEASUREMENTS)
//		{
//			if (time_selection==0 && i!=0) Draw_5x8_string(time_scale[time_selection][i],strlen(time_scale[time_selection][i]),((GRAPH_RIGHT-GRAPH_LEFT)*i)/4,TIME_HEIGHT,clr);
//			if (time_selection==1 && i!=0) Draw_5x8_string(time_scale[time_selection][i],strlen(time_scale[time_selection][i]),((GRAPH_RIGHT-GRAPH_LEFT)*(2*i-1))/6,TIME_HEIGHT,clr);
//			if (time_selection==2 && (i==1||i==3)) Draw_5x8_string(time_scale[time_selection][i],strlen(time_scale[time_selection][i]),((GRAPH_RIGHT-GRAPH_LEFT)*(i))/4,TIME_HEIGHT,clr);
//			if (time_selection==3) Draw_5x8_string(time_scale[time_selection][i],strlen(time_scale[time_selection][i]),((GRAPH_RIGHT-GRAPH_LEFT)*(i))/4,TIME_HEIGHT,clr);
//		}
//	}
//}
//
//void ClearText(void)
//{
//	int i;
//	int j;
//	for (i=0;i<127;i++)
//	{
//		for (j=0;j<32;j++) text_buff[j][i] = 0;
//	}
//}
//
//void UpdateText(unsigned char text_row,unsigned char text_index,unsigned char clr)
//{
//	int i;
//	int j;
//	char val_str[6];
//	for (i=0;i<=127;i++)
//	{
//		for (j=text_row;j<text_row+12;j++) text_buff[j][i] = 0;
//	}
//	
//	if (text_index == CLOCK_INDEX)
//	{
//		sprintf(val_str,"%02d:%02d",HOUR,MIN);
//		val_disp[text_index] = val_str;
//	}
//	
//	if (text_index == var_selection) 
//	{
//		Write_8x12_string(var_menu[text_index],strlen(var_menu[text_index]),TEXT_OFFSET,text_row,clr);
//		Write_8x12_string(val_disp[text_index],strlen(val_disp[text_index]),OLED_END - strlen(val_disp[text_index])*9,text_row,clr);
//	}
//	else
//	{
//		Write_8x12_string(var_menu[text_index],strlen(var_menu[text_index]),TEXT_OFFSET,text_row,clr&(0xB6));
//		Write_8x12_string(val_disp[text_index],strlen(val_disp[text_index]),OLED_END - strlen(val_disp[text_index])*9,text_row,clr&(0xB6));
//	}
//}
//
//void DrawVariables(void)
//{	
//	int row,col;
//	for (row=0;row<=TEXT_HEIGHT;row++)
//	{
//		for (col=0;col<=127;col++)
//		{
//			disp_buff[row][col] = (text_buff[(_current_row+row)%60][col]);
//		}
//	}
//	BufferToScreen(0,0,127,TEXT_HEIGHT);
//}

void BarGraphValues(unsigned int _values[],unsigned char num_vals, unsigned char _clr)
{
	int i;
	char _num_string[15];
	for (i=1;i<=num_vals;i++)
	{
		if (_values[i]==_values[0])	DrawRectangle(GRAPH_LEFT+(i-1)*(unsigned int)GRAPH_RIGHT/num_vals,GRAPH_END-(_values[i]*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],GRAPH_LEFT+i*(unsigned int)GRAPH_RIGHT/num_vals,GRAPH_END,_clr<<1,_clr<<1);
		else DrawRectangle(GRAPH_LEFT+(i-1)*(unsigned int)GRAPH_RIGHT/num_vals,GRAPH_END-(_values[i]*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],GRAPH_LEFT+i*(unsigned int)GRAPH_RIGHT/num_vals,GRAPH_END,_clr,_clr);	//0x9CF3 or 7BEF
	}
	sprintf(_num_string,"max=%d",_values[0]);
}

void LineGraphValues(unsigned int _values[],unsigned char num_vals, unsigned char _clr)
{
	int i;
	char _num_string[4];
	
	for (i=1;i<=num_vals;i++)
	{
		if (_values[i]==_values[0]) DrawLine(GRAPH_LEFT+((i-1)*(unsigned int)GRAPH_RIGHT/num_vals),GRAPH_END-(_values[i-1]*(i>1)*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],GRAPH_LEFT+((i)*(unsigned int)GRAPH_RIGHT/num_vals),GRAPH_END-(_values[i]*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],_clr);
		else DrawLine(GRAPH_LEFT+((i-1)*(unsigned int)GRAPH_RIGHT/num_vals),GRAPH_END-(_values[i-1]*(i>1)*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],GRAPH_LEFT+((i)*(unsigned int)GRAPH_RIGHT/num_vals),GRAPH_END-(_values[i]*(unsigned int)(GRAPH_END-GRAPH_START-12))/_values[0],_clr&0xDB);
	}
	
	sprintf(_num_string,"max=%d",_values[0]);
}